<?php
namespace Admin\Controller;
use Think\Controller;
use Admin\Model\TeacherModel;

class LoginController extends Controller{
	public function login(){
        if (session('?adminId')){
            if (session('?adminType')){
                if(session('adminType') == 2){
                    $this->redirect('Index/index','', 0);
                }else{
                    $this->redirect('IndexTeacher/index','', 0);
                }
            }
        }else{
            $this->display();
        }

    }

    /*
     *登录表单提交处理
     */
    public function loginInfo(){
        try{
            $admin = new TeacherModel();
            if ($data=$admin->create()){
                $info = $admin->where(['teacher_name'=>$data['teacher_name'],'password'=>$data['password']])->find();
                if  ($info){
                        session('adminId',$info['teacher_id']);
                        session('adminName',$info['teacher_name']);
                        session('adminType',$info['type']);
                        //设置会话开始时间
                        session('time',date("Y-m-d H:i:s",time()));

                        $teacher_id = $_SESSION['adminId'];//用户ID
                        $teacher_name = $_SESSION['adminName'];//用户名    
                        $this->UserLoginLog('登录系统',$teacher_id,$teacher_name); 
                        
                        if($info['type'] == 1){
                            $this->success("登录成功",U('IndexTeacher/index'),1);
                        }else if($info['type'] == 2){
                            $this->success("登录成功",U('Index/index'),1);
                        }
                        
                }else{
                    $this->error("登录名或者密码错误",'login',1);
                }
            }
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }

    /*
     * 注销
     * */
    public function logout(){
        if (session('?adminId')){
            $teacher_id = $_SESSION['adminId'];//用户ID 
            $teacher_name = $_SESSION['adminName'];//用户名 
            $this->UserLoginLog('退出系统',$teacher_id,$teacher_name); 

            session_destroy();
            $this->success("注销成功",U('login'),0);
        }else{
            $this->redirect('login','', 0);
        }
    }
    /*
     * 生成验证码
     */
    public function verify_c(){
        $Verify = new Verify();
        $Verify->fontSize = 18;
        $Verify->length   = 4;
        $Verify->useNoise = true;
        $Verify->useZh = true;

        $Verify->zhSet = '中文验证码输验中文证入验中文中文证后台管理实训项目中国东莞理学院理工许愿后中文台验证';
//        $Verify->imageW = 130;
        $Verify->imageH = 38;
//        $Verify->expire = 600;
        $Verify->entry();
    }
    /*
     * 验证码验证
     * */
    public function check_verify($code, $id = ""){
        $verify = new Verify();
        return $verify->check($code, $id);
    }

    // 系统操作日志
    public function UserLoginLog($log,$teacher_id,$teacher_name){  
        $logModel = M('Log'); //实例化模型  
        $data = array(
            'teacher_id' => $teacher_id, //用户ID    
            'teacher_name' => $teacher_name, //用户ID  
            'op_log' => $log, //操作内容  
            'op_time' => time() //操作时间(当前系统时间)  
        );  
        $logModel->add($data);  
    }  

}